package com.loginregister.web;

import com.loginregister.pojo.User;
import com.loginregister.service.UserService;
import com.loginregister.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wzm
 * @create 2023-05-13 14:46
 */
//该类用于书写关于User所有Servlet功能
@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();



     protected void login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //实现请求的处理和响应
        //1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //2.获取页面参数(返回值是String类型 如果涉及 年龄 id 需要转换类型)
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //获取记住我选框
        String remember = request.getParameter("remember");

        User user = userService.login(new User(username, password));

        if (user != null){
            //查询成功
            if(user.getStatus() == 0){
                request.setAttribute("login_msg","用户被禁用");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
                return;
            }

            if ("1".equals(remember)){
                //勾选了记住我
                //将登录信息存储到cookie中
                Cookie uname = new Cookie("username",username);
                Cookie pwd = new Cookie("password",password);

                //cookie发送数据给服务器是有要求的  只是在当前路径下资源访问才可以
                /*如 username这个cookie 存在于 http://ip:端口/a/login
                那么只要路径不带/a/都无法传输cookie到servlet
                所以我们将cookie的过来路径直接设置为当前项目 不区分路径
                 */

//                uname.setPath(request.getContextPath());
//                pwd.setPath(request.getContextPath());

                //七天存储时间
                uname.setMaxAge(60*60*24*7);
                pwd.setMaxAge(60*60*24*7);

                //通知浏览器保存
                response.addCookie(uname);
                response.addCookie(pwd);
            }

            //不存储登录信息
            //将登录后的用户存储起来 在整个网页中能够使用
            request.getSession().setAttribute("loginUser",user);
            response.sendRedirect(request.getContextPath()+"/index.jsp");

        }else{
            //失败
            //将错误信息存储
            request.setAttribute("login_msg","用户名或密码错误，请重试");

            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }


    }

    protected void register(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        //获取参数  封装对象 对比验证码  查询结果  存储结果返回
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取 用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //验证码
        String code = request.getParameter("checkCode");

        //获取存储在session中的验证码
        String seesioncode = (String) request.getSession().getAttribute("seesioncode");

        //对比验证  出错则返回
        if(!code.equalsIgnoreCase(seesioncode)){
            //对比失败
            request.setAttribute("reg_msg","验证码错误");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;//停止当前方法
        }

        //验证码正确
        boolean register = userService.register(new User(username, password));
        if(register){
            //注册成功去登录
            request.setAttribute("login_msg","恭喜，注册成功，请登录！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else{
            //注册失败
            request.setAttribute("reg_msg","数据同步错误，稍后重试！");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }
    }
}
